/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.util;

import de.teamskill.pux.engine.DataElement;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.OutputPort;

/**
 * MultiplexerNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class MultiplexerNode extends AbstractNode {

	/**
	 * OUT_DATA
	 */
	public static final String OUT_DATA = "DATA";

	/**
	 * IN_DATA
	 */
	public static final String IN_DATA = "DATA";

	/**
	 * channels
	 */
	private int channels;

	/**
	 * out
	 */
	private OutputPort out;

	/**
	 * MultiplexerNode Constructor
	 * 
	 */
	public MultiplexerNode() {
		this("MUX" + nr++, false);
	}

	/**
	 * MultiplexerNode Constructor
	 * 
	 * @param name
	 * @param continuous
	 */
	public MultiplexerNode(String name, boolean continuous) {
		this(name, 2, continuous);
	}

	/**
	 * MultiplexerNode Constructor
	 * 
	 * @param name
	 * @param channels
	 * @param continuous
	 */
	public MultiplexerNode(String name, int channels, boolean continuous) {
		super(name, continuous);
		this.channels = channels;
	}

	/**
	 * @see de.teamskill.pux.Node#compute()
	 */
	@Override
	public void compute() {
		for (int i = 0; i < channels; i++) {
			InputPort ip = getInputPort(IN_DATA + i);
			if (ip.hasData()) {
				DataElement de = ip.fetchBuffer();
				out.pushBuffer(de);
			}
		}
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		for (int i = 0; i < channels; i++) {
			addInputPort(IN_DATA + i, null, false);
		}
		out = addOutputPort(new OutputPort(OUT_DATA, this, null));
	}

	/**
	 * JavaBean-style getter-method for channels
	 * 
	 * @return the channels
	 */
	public int getChannels() {
		return channels;
	}

	/**
	 * /** JavaBean-style setter-method for channels
	 * 
	 * @param channels
	 *            the channels to set
	 */
	public void setChannels(int channels) {
		this.channels = channels;
	}

	/**
	 * JavaBean-style getter-method for out
	 * 
	 * @return the out
	 */
	public OutputPort getOut() {
		return out;
	}

	/**
	 * /** JavaBean-style setter-method for out
	 * 
	 * @param out
	 *            the out to set
	 */
	public void setOut(OutputPort out) {
		this.out = out;
	}

}
